/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latesis.dao.hibernate;

import com.latesis.bean.MensajeBean;
import com.latesis.bean.ServicioBean;
import com.latesis.bean.ServicioBean;
import com.latesis.dao.ServicioDAO;
import com.latesis.util.Constantes;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author acabrera
 */
@Transactional(propagation=Propagation.SUPPORTS)
public class ServicioDAOHibernateImpl  extends HibernateDaoSupport implements ServicioDAO{
    public ServicioBean guardar(ServicioBean bean) {
         getHibernateTemplate().saveOrUpdate(bean);
         return bean;
    }

    public void borrar(ServicioBean bean) {
        getHibernateTemplate().delete(bean) ;
    }

    public ServicioBean buscar(ServicioBean bean) {
        return getHibernateTemplate().get(ServicioBean.class, bean.getId());
    }

    public List<ServicioBean> buscarTodos() {
        return (List<ServicioBean>)getHibernateTemplate().find("from ServicioBean ");
    }

    public List<ServicioBean> buscarPendientes(Long idChofer, Long idMovil) {
        return (List<ServicioBean>)getHibernateTemplate().find(
                      "from ServicioBean where chofer.id=? and movil.id=? and finalizado=0",idChofer,idMovil
                );        
    }

    public List<ServicioBean> buscarAsignados(Long idChofer, Long idMovil) {
        //return (List<ServicioBean>)getHibernateTemplate().find("from ServicioBean s where s.chofer is null ");
        return (List<ServicioBean>)getHibernateTemplate().find("from ServicioBean s where s.finalizado=0 and s.chofer is null and (?+'-'+? in (select a.chofer.id+'-'+a.movil.id from s.asignaciones a where a.respuesta is null))",""+idChofer,""+idMovil);
                //+""
                //+ "and (s.id in (select a.id from s.asignaciones g inner join g.choferes as c))"
                //,Constantes.TIPO_GRUPO_CHOFERES,idChofer);     
        //return (List<ServicioBean>)getHibernateTemplate().find("from ServicioBean s where s.grupoAsignacion is not null "+""
        //        + "and (s.grupoAsignacion.tip = ? and ? in (select c.id from s.grupoAsignacion g inner join g.choferes as c))"
        //        ,Constantes.TIPO_GRUPO_CHOFERES,idChofer);        
        //return (List<MensajeBean>)getHibernateTemplate().find("from MensajeBean m where m.id>? and ? in (select c.id from MensajeBean m2 inner join m2.choferes as c )",maximoId, idChofer);        
    }
}
